package com.quectel.taskserver.job.partybuilding;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.business.common.partybuilding.PartyPersonMsgBusiness;
import com.quectel.constant.PushConstants;
import com.quectel.constant.core.mobileuser.MobileUserMsgConstants;
import com.quectel.constant.core.partybuilding.PartyBuildingConstants;
import com.quectel.constant.core.partyeducationexam.PartyEducationExamConstants;
import com.quectel.core.module.mobileuser.dto.MobileUserDto;
import com.quectel.core.module.mobileuser.service.MobileUserService;
import com.quectel.core.module.partyeducationexam.dto.PartyEducationTrainingExamDto;
import com.quectel.core.module.partyeducationexam.dto.PartyEducationTrainingExamPersonDto;
import com.quectel.core.module.partyeducationexam.dto.ref.PersonRefExamDto;
import com.quectel.core.module.partyeducationexam.service.PartyEducationTrainingExamPersonService;
import com.quectel.core.module.partyeducationexam.service.PartyEducationTrainingExamService;
import com.quectel.util.kit.PageFetchKit;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 党建活动相关消息推送
 *
 * @author: bob
 * @email: bob.yu@quectel.com
 * @date: 2023-06-12 8:59:00
 */
@Component
public class PartyBuildingExamMsgPushTask implements SimpleJob {

    private static final Logger LOGGER = LoggerFactory.getLogger(PartyBuildingExamMsgPushTask.class);

    @DubboReference
    private PartyEducationTrainingExamService partyEducationTrainingExamService;
    @DubboReference
    private PartyEducationTrainingExamPersonService partyEducationTrainingExamPersonService;
    @DubboReference
    private MobileUserService mobileUserService;
    @Autowired
    private PartyPersonMsgBusiness partyPersonMsgBusiness;

    @Override
    public void execute(ShardingContext shardingContext) {
        exec();
    }

    private void exec() {
        Map<String, Object> params = new HashMap<>();
        params.put("timeToPush", "1");
        params.put(LambdaUtil.getFieldName(PartyEducationTrainingExamDto::getStatus), PartyEducationExamConstants.StatusEnum.PUBLISHED.getStatus());
        params.put(LambdaUtil.getFieldName(PartyEducationTrainingExamDto::getPushStatus), PartyEducationExamConstants.PushStatusEnum.UNPUBLISHED.getStatus());

        List<PartyEducationTrainingExamDto> partyEducationTrainingExamDtos = PageFetchKit.fetchAll(req -> partyEducationTrainingExamService.queryList(req)
                , params, 500);

        for (PartyEducationTrainingExamDto partyEducationTrainingExamDto : partyEducationTrainingExamDtos) {
            Map<String, Object> queryPersonParams = new HashMap<>();
            queryPersonParams.put(LambdaUtil.getFieldName(PartyEducationTrainingExamPersonDto::getExamId), partyEducationTrainingExamDto.getId());
            List<PersonRefExamDto> personRefExamDtos = PageFetchKit.fetchAll(req -> partyEducationTrainingExamPersonService.queryPersonList(req), queryPersonParams, 500);
            List<MobileUserDto> mobileUserDtos = personRefExamDtos.parallelStream()
                    .map(o -> mobileUserService.selectByMobile(o.getMobile()))
                    .filter(Objects::nonNull).collect(Collectors.toList());

            partyPersonMsgBusiness.pushMsg(
                    partyEducationTrainingExamDto.getTenantId(),
                    mobileUserDtos,
                    MobileUserMsgConstants.SourceType.PARTY_BUILDING_TRAINING_EXAM,
                    partyEducationTrainingExamDto.getId(),
                    PartyBuildingConstants.PARTY_BUILDING_TRAINING_EXAM_MSG_TITLE,
                    partyEducationTrainingExamDto.getName(),
                    true,
                    new PushConstants.PushType[]{PushConstants.PushType.WECHAT_OFFICIAL}
            );

            PartyEducationTrainingExamDto newlyDto = new PartyEducationTrainingExamDto();
            newlyDto.setId(partyEducationTrainingExamDto.getId());
            newlyDto.setPushStatus(PartyEducationExamConstants.PushStatusEnum.PUBLISHED.getStatus());
            partyEducationTrainingExamService.updateById(newlyDto);
        }
    }
}
